package com.wsoft.bpm.service;

import cn.hutool.core.util.StrUtil;
import com.wsoft.bpm.component.command.OutgoingJumpMultiCmd;
import com.wsoft.bpm.component.command.TaskJumpCmd;
import com.wsoft.bpm.component.command.TaskJumpMultiCmd;
import com.wsoft.core.exception.Asserts;
import com.wsoft.core.utils.LoginUserUtil;
import com.wsoft.vo.LoginUserVO;
import org.activiti.engine.ManagementService;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 动态节点跳过服务
 *
 * @Author zhonglj
 * @Date 2024-10-08 11:15
 */
@Service
public class DynamicJumpService {
    @Resource
    protected ManagementService managementService;
    @Resource
    private TaskService taskService;

    /**
     * 跳转至任意节点 多目标节点
     *
     * @param taskId
     * @param targetNodes
     * @param comment
     * @param comment
     * @author zhonglj
     * @date 2024-10-08 13:24
     **/
    @Transactional(rollbackFor = Exception.class)
    public void jumpTaskMultiFromProcess(String taskId, List<String> targetNodes, String comment) {
        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
        LoginUserVO loginUserVO = LoginUserUtil.getUserInfo().getLoginUserVO();

        if (task.getAssignee() == null) {
            // 拾取任务
            taskService.claim(task.getId(), loginUserVO.getId().toString());
        }
        if (StrUtil.isNotBlank(comment)) {
            taskService.addComment(task.getId(), task.getProcessInstanceId(), comment);
        }
        Boolean executed = managementService.executeCommand(new TaskJumpMultiCmd(taskId, targetNodes));
        if (!executed) {
            Asserts.fail("[bpm.jump.fail]");
        }

    }

    /**
     * 跳转至任意节点
     *
     * @param taskId
     * @param targetNode
     * @param comment
     * @param comment
     * @author zhonglj
     * @date 2024-10-08 13:24
     **/
    @Transactional(rollbackFor = Exception.class)
    public void jumpTaskFromProcess(String taskId, String targetNode, String comment) {
        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
        if (StrUtil.isNotBlank(comment)) {
            taskService.addComment(task.getId(), task.getProcessInstanceId(), comment);
        }
        Boolean executed = managementService.executeCommand(new TaskJumpCmd(taskId, targetNode, null));
        if (!executed) {
            Asserts.fail("[bpm.jump.fail]");
        }
    }

    /**
     * 使用outgoing属性实现跳转
     *
     * @param taskId
     * @param targetNodes
     * @param comment
     * @return void
     * @author zhonglj
     * @date 2024/12/9 9:43
     **/
    @Transactional(rollbackFor = Exception.class)
    public void outgoingJump(String taskId, List<String> targetNodes, String comment) {
        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
        if (StrUtil.isNotBlank(comment)) {
            taskService.addComment(task.getId(), task.getProcessInstanceId(), comment);
        }
        Boolean executed = managementService.executeCommand(new OutgoingJumpMultiCmd(taskId, targetNodes));
        if (!executed) {
            Asserts.fail("[bpm.jump.fail]");
        }
    }
}